<?php
$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();
$model = $this->LoadModel('Public');
$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();
$this->LoadHelper('PublicHelper');
$PublicHelper = new PublicHelper();
$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();
$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
$report_names = array('unsecured','carfare','overdue','compensatory','advance','telaudit','proorder','contract','gps','carreport','brstandard');
if(isset($this->Param['option'])){
	if (!in_array($this->Param['option'],$report_names)) {
	    echo $this->GetView('index.php');
	}else{
		$this->Assign('option', $this->Param['option']);
    if($this->Param['option'] == 'carreport'){
      $bank = $model->getList('bank',array('bk_name'),array('`bk_status` =?' => 'ok'));
      $this->Assign('bank',$bank);
    }
		echo $this->GetView('report_detail.php');
	}
}else{
	$ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if(isset($this->Param['start']) && isset($this->Param['end'])){
      if($ajax == 'unsecured'){
         	$where = array(
         		'`o_bank_money`>?' => 0,
         		'`o_dl_date` = ? OR o_dl_date IS NULL'=> 0,
         		'`o_status` NOT IN(?)' => array('退单','作废','已结清','已代偿')
        	);
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_bank_time` = ?'] = $start;
          }else{
            $where['`o_bank_time` >= ?'] = $start;
            $where['`o_bank_time` < ?'] = $end;
          }
        }else{
          $where['`o_bank_time` > ?'] = 0;
        }
        	$data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'postloan_info',array('o_bank_time ASC'));
        	$json_data = array('total' => $data['count'], 'rows' => array());
          if (!empty($data['all']))
              foreach ($data['all'] AS $rowNum => $row) {
              	$row['o_pledge_todate'] = empty($row['o_pledge_todate'])? '':date('Y-m-d', strtotime($row['o_pledge_todate']));
                $row['o_bank_time'] = empty($row['o_bank_time'])? '':date('Y-m-d', strtotime($row['o_bank_time']));
                $row['o_pact_totime'] = empty($row['o_pact_totime'])? '':date('Y-m-d', strtotime($row['o_pact_totime']));
                $row['o_pledge_towgdate'] = empty($row['o_pledge_towgdate'])? '':date('Y-m-d', strtotime($row['o_pledge_towgdate']));
                $json_data['rows'][] = $row;
              }
          echo json_encode($json_data);
      }
      elseif($ajax == 'carfare' || $ajax == 'contract'){
      	$where = array(
      		'`o_adtime` >?' => 0 ,
      		'`o_ad_money` >?' => 0 ,
      		'`o_status` NOT IN (?)' => array('退单','作废'),
      		'`o_pact_totime` = ? OR o_pact_totime IS NULL'=> 0
      	);
        if($ajax == 'contract'){
          $where['`o_pact_gettime` =? OR o_pact_gettime IS NULL'] = 0;
        }
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_adtime` = ?'] = $start;
          }else{
            $where['`o_adtime` >=?'] = $start;
            $where['`o_adtime` <?'] = $end;
          }
        }
      	$data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'postloan_info',array('o_adtime ASC'));
        	$json_data = array('total' => $data['count'], 'rows' => array());
        	if (!empty($data['all']))
              foreach ($data['all'] AS $rowNum => $row) {
              	$row['o_adtime'] = date('Y-m-d', strtotime($row['o_adtime']));
                $row['o_pact_totime'] = empty($row['o_pact_totime'])? '': date('Y-m-d', strtotime($row['o_pact_totime']));
                $row['o_reg_godate'] = empty($row['o_reg_godate'])? '':date('Y-m-d', strtotime($row['o_reg_godate']));
                $row['o_invoice_date'] = empty($row['o_invoice_date'])? '':date('Y-m-d', strtotime($row['o_invoice_date']));
                $row['o_travel_date'] = empty($row['o_travel_date'])? '':date('Y-m-d', strtotime($row['o_travel_date']));
                $row['o_cprice_date'] = empty($row['o_cprice_date'])? '':date('Y-m-d', strtotime($row['o_cprice_date']));
                $row['o_qualified_date'] = empty($row['o_qualified_date'])? '':date('Y-m-d', strtotime($row['o_qualified_date']));
                $row['o_warranty_date'] = empty($row['o_warranty_date'])? '':date('Y-m-d', strtotime($row['o_warranty_date']));
                $json_data['rows'][] = $row;
              }
          echo json_encode($json_data);
      }
      elseif($ajax == 'overdue'){
        $where = array(
          '`o_total_num` >?' => 0
        );
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_bank_time` = ?'] = $start;
          }else{
            $where['`o_bank_time` >=?'] = $start;
            $where['`o_bank_time` <?'] = $end;
          }
        }else{
          $where['`o_bank_time` >?'] = 0;
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'postloan_info',array('o_bank_time ASC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              $row['o_bank_time'] = empty($row['o_bank_time'])? '':date('Y-m-d', strtotime($row['o_bank_time']));
              $row['o_serial_num1'] = $row['o_lastover_money']>0 ? $row['o_serial_num']: 0 ;
              $row['o_lastover_money'] = $row['o_lastover_money']> 0 ? $row['o_lastover_money'] :'';
              $row['o_return_time'] = empty($row['o_return_time'])? '':date('Y-m-d', strtotime($row['o_return_time']));
              $row['o_tc_mercy_date'] = empty($row['o_tc_mercy_date'])? '':date('Y-m-d', strtotime($row['o_tc_mercy_date']));
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
  
      }
      elseif($ajax == 'compensatory'){
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
              $where['`o_tc_complete_date` = ?'] = $start;
          }else{
            $where['`o_tc_complete_date` >=?'] = $start;
            $where['`o_tc_complete_date` <?'] = $end;
          }
        }else{
          $where['`o_tc_complete_date` >?'] = 0;
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'postloan_info',array('o_tc_complete_date ASC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              $row['o_tc_complete_date'] = empty($row['o_tc_complete_date'])? '':date('Y-m-d', strtotime($row['o_tc_complete_date']));
              $row['o_compensatory'] = abs($row['o_compensatory']);
              $row['o_tc_complete_bank1'] = $row['o_tc_complete_bank1'] == $row['o_tc_complete_bank2']? $row['o_tc_complete_bank1'] : '' ;
              $row['o_deposit'] = abs($row['o_deposit']);
              $row['o_tc_handle_date'] = empty($row['o_tc_handle_date'])? '':date('Y-m-d', strtotime($row['o_tc_handle_date']));
              $row['o_tc_mercy_date'] = empty($row['o_tc_mercy_date'])? '':date('Y-m-d', strtotime($row['o_tc_mercy_date']));
              $row['o_start_date'] = empty($row['o_start_date'])? '':date('Y-m-d', strtotime($row['o_start_date']));
              $row['o_judge_date'] = empty($row['o_judge_date'])? '':date('Y-m-d', strtotime($row['o_judge_date']));
              $row['o_effect_date'] = empty($row['o_effect_date'])? '':date('Y-m-d', strtotime($row['o_effect_date']));
              $row['o_exe_date'] = empty($row['o_exe_date'])? '':date('Y-m-d', strtotime($row['o_exe_date']));
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
  
      }
      elseif($ajax == 'advance'){
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_adtime` = ?'] = $start;
          }else{
            $where['`o_adtime` >=?'] = $start;
            $where['`o_adtime` <?'] = $end;
          }
        }else{
          $where['`o_adtime` >?'] = 0;
        }
  
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info,'postloan_info',array('o_tc_complete_date ASC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
              $row['o_adtime'] = empty($row['o_adtime'])? '':date('Y-m-d', strtotime($row['o_adtime']));
              $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
      }
      elseif($ajax == 'telaudit'){
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = strtotime($this->Param['start']);
          $end = strtotime($this->Param['end']);
          $end = $end + 86400;
          $start = date('Y-m-d H:i:s',$start);
          $end = date('Y-m-d H:i:s',$end);
          $where['`a_dateline` >= ?'] = $start;
          $where['`a_dateline` <?'] = $end;
        }else{
          $where['`a_dateline` >?'] = 0;
        }
        if($member_info['u_role'] == 8){
          $where['`a_uname` =?'] = $member_info['u_realname'];
        } 
        $data = $OrderHelper->orderList($where,$page,$rp,$this->Param,$member_info,'order_telaudit',array('a_dateline ASC'));
        $json_data = array('total' => $data['count'],'rows' =>array());
        if(!empty($data['all']))
          foreach ($data['all'] AS $rowNum => $row){
            $row['o_telstart_time'] = empty($row['o_telstart_time'])? '':date('Y-m-d H:i:s', $row['o_telstart_time']);
            $json_data['rows'][] = $row;
          }
        echo json_encode($json_data);
      }
      elseif($ajax == 'proorder'){
        $where = array(
          '`o_mp_quit_status` = ?' =>'quit',
          '`o_mp_deal_status` = ?' =>'ok',
          '`o_adtime` >?' =>0,
          '`o_bank_time` = ? OR o_bank_time IS NULL' => 0,
        );
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_adtime` = ?'] = $start;
          }else{
            $where['`o_adtime` >=?'] = $start;
            $where['`o_adtime` <?'] = $end;
          }
        }else{
          $where['`o_adtime` >?'] = 0;
        }
        $data = $OrderHelper->orderList($where,$page,$rp,$this->Param,$member_info,'postloan_info',array('o_mp_quit_fortime ASC'));
        $json_data = array('total' => $data['count'],'rows' =>array());
        if(!empty($data['all']))
          foreach ($data['all'] AS $rowNum => $row){
            $row['o_mp_quit_fortime'] = empty($row['o_mp_quit_fortime'])? '':date('Y-m-d', strtotime($row['o_mp_quit_fortime']));
            $row['o_adtime'] = empty($row['o_adtime'])? '':date('Y-m-d', strtotime($row['o_adtime']));
            $json_data['rows'][] = $row;
          }
        echo json_encode($json_data);
        
      }
      elseif($ajax == 'gps'){
        $where = array(
          '`o_adtime` >?' => 0,
          '`o_ad_money` >?' => 0,
          '`o_status` NOT IN (?)' => array('退单','作废'),
          '`o_gps_status` != ?' => '已上线',
          '`o_needgps` =?' =>1
        );
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = str_replace('-','',$this->Param['start']);
          $end = str_replace('-','',$this->Param['end']);
          if($start == $end){
            $where['`o_adtime` = ?'] = $start;
          }else{
            $where['`o_adtime` >=?'] = $start;
            $where['`o_adtime` <?'] = $end;
          }
        }
        $data = $OrderHelper->orderList($where,$page,$rp,$this->Param,$member_info,'postloan_info',array('o_adtime ASC'));
        $json_data = array('total' => $data['count'],'rows' =>array());
        if(!empty($data['all']))
          foreach ($data['all'] AS $rowNum => $row){
            $row['o_gps_install'] = $row['o_gps_install'] == '0000-00-00'? '':$row['o_gps_install'];
            $row['o_adtime'] = empty($row['o_adtime'])? '':date('Y-m-d', strtotime($row['o_adtime']));
            $json_data['rows'][] = $row;
          }
        echo json_encode($json_data);

      }
      elseif($ajax == 'carreport'){
        $where = array(
          '`o_report_downtime` >?' => 0
        );
        if(strlen($this->Param['start'])>1 && strlen($this->Param['end'])>1){
          $start = strtotime(str_replace('-','',$this->Param['start']));
          $end = strtotime(str_replace('-','',$this->Param['end']));
          if($start == $end){
            $where['`o_report_downtime` = ?'] = $start;
          }else{
            $where['`o_report_downtime` >=?'] = $start;
            $where['`o_report_downtime` <?'] = $end;
          }
        }
        if($this->Param['bank'] != '全部'){
          $where['`o_bank` =?'] = $this->Param['bank'];
        }
        if($member_info['u_role'] ==  13){
          $where['`o_report_downuser` =?'] = $member_info['u_realname'];
        } 
        $data = $OrderHelper->orderList($where,$page,$rp,$this->Param,$member_info,'order',array('o_report_downtime ASC'));
        $json_data = array('total' => $data['count'],'rows' =>array());
        if(!empty($data['all']))
          foreach ($data['all'] AS $rowNum => $row){
            $row['o_report_downtime'] = date('Y-m-d', $row['o_report_downtime']);
            $car_row = $model->getOneRow('order_car','*',array('`ot_oid` =?' =>$row['o_id']));
            $row['o_sculpt'] = $car_row['ot_car_model'];
            $json_data['rows'][] = $row;
          }
        echo json_encode($json_data);

      }
      elseif($ajax =='brstandard'){
        $where = array(
          '`br_status` =?' =>'ok'
        );

        $data = $BranchHelper->branchScreen($where,$member_info,$page,$rp);
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
          foreach ($data['all'] AS $rowNum => $row) {
            $data = $BranchHelper->getOtherMsg($row['br_id'],$row['br_name']);
            $row['br_yuqi'] = is_numeric($data['br_yuqi']) ? $data['br_yuqi'].'%': $data['br_yuqi'];
            $row['br_front_yuqi'] = is_numeric($data['br_front_yuqi']) ? $data['br_front_yuqi'].'%' : $data['br_front_yuqi']; 
            $row['br_ffront_yuqi'] = is_numeric($data['br_ffront_yuqi']) ? $data['br_ffront_yuqi'].'%' : $data['br_ffront_yuqi']; 
            $row['br_first_contracttime'] = $data['br_first_contracttime'];
            if(!empty($row['br_audit_details'])){
              $row['br_present_standard'] = $row['br_audit_details'];
            }else{
              if($row['br_audit_value']==0){
                $row['br_present_standard'] = '无简审';
              }elseif($row['br_audit_value'] >= 10){
                $row['br_present_standard'] = $row['br_audit_value'].'万（含）以下简审';
              }else{
                $row['br_present_standard'] = $row['br_audit_value'].'万（含）以下双免简审';
              }
            }
            $json_data['rows'][] = $row;
          }
        echo json_encode($json_data);
      }
  }
}
if($this->Param['act'] == 'excel'){
    $msg = stripslashes($this->Param['report_msg']);
    $column = stripslashes($this->Param['coulumns_msg']);
    $column = json_decode($column,true);
    unset($column['numberer']);
    $report_msg = explode('}{',$msg);
    $count = count($report_msg);
    if($count == 1){
      $report_msg[0] = json_decode($report_msg[0],true);
    }else{
      for($i=0;$i<$count;$i++){
        if($i==0){
          $report_msg[$i] = $report_msg[$i].'}';
        }elseif($i == ($count-1)){
          $report_msg[$i] = '{'.$report_msg[$i];
        }else{
          $report_msg[$i] = '{'.$report_msg[$i].'}';
        }
        $report_msg[$i] = json_decode($report_msg[$i],true);
      }
    }
    $PublicHelper->downReport($report_msg,$this->Param['report_name'],$column);  
}

